package com.mediastory.spsserver.verMngr.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mediastory.spsserver.verMngr.dao.VerMngrDao;
import com.mediastory.spsserver.verMngr.domain.VerMngr;
import com.mediastory.spsserver.verMngr.service.VerMngrSvc;

/**
 * @author Jaewon Choi
 *
 */

@Service
@Transactional
public class VerMngrSvcImpl implements VerMngrSvc {

	@Autowired
	VerMngrDao verMngrDao;
	
	public VerMngrSvcImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<VerMngr> findbyList(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return (List<VerMngr>) (verMngrDao.SelectList(condition));
	}

	public Map<String, Object> findbyListPaging(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("verMngrList", (List<VerMngr>) (verMngrDao.SelectList(condition)));
		resultMap.put("verMngrListTotalCnt", (Integer) (verMngrDao.SelectListTotalCnt(condition)));
		
		return resultMap;
	}

	@Override
	public VerMngr findby(Integer idx) throws Exception {
		// TODO Auto-generated method stub
		return verMngrDao.select(idx);
	}

	@Override
	public VerMngr add(VerMngr verMngr) throws Exception {
		// TODO Auto-generated method stub
		
		VerMngr result = null;
		
		//기존 데이터 삭제
		Map<String, Object> condition = new HashMap<String, Object>();
		verMngrDao.delete(condition);
		
		//신규 등록
		VerMngr vm = null;
		for(int i =0;i<verMngr.getMarkets().length;i++){
			vm = new VerMngr();
			String market = verMngr.getMarkets()[i];
			Integer code = verMngr.getCodes()[i];
			String viewCode = verMngr.getViewCodes()[i];
			String url = verMngr.getUrls()[i];
			
			vm.setMarket(market);
			vm.setCode(code);
			vm.setViewCode(viewCode);
			vm.setUrl(url);
			vm.setCreatedId(verMngr.getCreatedId());
			vm.setCreatedIp(verMngr.getCreatedIp());
			result = verMngrDao.insert(vm);
		}
		
		return result;
	}

	@Override
	public VerMngr modify(VerMngr verMngr) throws Exception {
		// TODO Auto-generated method stub
		return verMngrDao.update(verMngr);
	}

	@Override
	public int remove(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		return verMngrDao.delete(condition);
	}

}